package cn.yeziji.forum.converter;

import cn.yeziji.forum.dto.comment.ForumDetailCommentDTO;
import cn.yeziji.forum.dto.comment.ForumOperationCommentDTO;
import cn.yeziji.forum.entity.ForumCommentEntity;
import cn.yeziji.forum.entity.ForumUserCommentEntity;
import cn.yeziji.forum.utils.StrUtils;
import cn.yeziji.forum.vo.comment.DetailCommentVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 评论转换器
 *
 * @author gzkemays
 * @since 2022/1/10 21:14
 */
@Mapper
public interface CommentConverter {
  CommentConverter INSTANCE = Mappers.getMapper(CommentConverter.class);

  /**
   * 文章评论值对象转换为 entity
   *
   * @param dto 值对象
   * @return entity
   */
  ForumCommentEntity detailDtoConverterEntity(ForumDetailCommentDTO dto);

  /**
   * 文章评论 entity 转化为文章评论返回值对象
   *
   * @param entity 源对象
   * @return 值对象
   */
  @Mapping(target = "createTime", source = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
  DetailCommentVO entityConverterDetailVo(ForumCommentEntity entity);

  /**
   * {@link #entityConverterDetailVo(ForumCommentEntity)} to list
   *
   * @param entities 源对象列表
   * @return 值对象列表
   */
  List<DetailCommentVO> entitiesConverterDetailVos(List<ForumCommentEntity> entities);

  /**
   * operation comment convert to self entity
   *
   * @param dto operation dto
   * @return entity {@link ForumUserCommentEntity}
   */
  ForumUserCommentEntity operationCommentConverterEntity(ForumOperationCommentDTO dto);

  default List<DetailCommentVO> mapConverterToDetailCommentVos(List<Map<String, Object>> maps) {
    if (maps == null || maps.isEmpty()) {
      return null;
    }
    List<DetailCommentVO> list = new ArrayList<DetailCommentVO>(maps.size());
    for (Map<String, Object> map : maps) {
      list.add(mapConverterToDetailCommentVo(map));
    }
    return list;
  }

  default DetailCommentVO mapConverterToDetailCommentVo(Map<String, Object> map) {
    DetailCommentVO vo = new DetailCommentVO();
    vo.setId((Integer) map.get("id"));
    vo.setUserId((Integer) map.get("userId"));
    vo.setAvatar((String) map.get("avatar"));
    vo.setNickname((String) map.get("nickname"));
    vo.setAt((String) map.get("at"));
    vo.setContent((String) map.get("content"));
    vo.setCreateTime(StrUtils.dateToStr("yyyy-MM-dd HH:mm:ss", (Date) map.get("createTime")));
    vo.setFollow((Long) map.get("follow"));
    vo.setGoodNumber(Long.parseLong(map.get("goodNumber").toString()));
    vo.setParentCommentId((Integer) map.get("parentCommentId"));
    vo.setToGood((Boolean) map.get("toGood"));
    return vo;
  }
}
